using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;

namespace OF.Security.Utils
{
	public static class EnvironmentHelper
	{
		private enum COMPUTER_NAME_FORMAT
		{
			ComputerNameNetBIOS,
			ComputerNameDnsHostname,
			ComputerNameDnsDomain,
			ComputerNameDnsFullyQualified,
			ComputerNamePhysicalNetBIOS,
			ComputerNamePhysicalDnsHostname,
			ComputerNamePhysicalDnsDomain,
			ComputerNamePhysicalDnsFullyQualified,
			ComputerNameMax
		}

		private enum EXTENDED_NAME_FORMAT
		{
			NameUnknown,
			NameFullyQualifiedDN,
			NameSamCompatible,
			NameDisplay,
			NameUniqueId = 6,
			NameCanonical,
			NameUserPrincipal,
			NameCanonicalEx,
			NameServicePrincipal,
			NameDnsDomain = 12
		}

		private static string shortDomainName;

		private static string domainDnsName;

		public static InstanceMode Mode
		{
			get
			{
				InstanceMode result;
				if (EnvironmentHelper.CheckIsWebApplication())
				{
					result = InstanceMode.Web;
				}
				else
				{
					result = InstanceMode.Windows;
				}
				return result;
			}
		}

		public static string ShortDomainName
		{
			get
			{
				return EnvironmentHelper.shortDomainName;
			}
		}

		public static string DomainDnsName
		{
			get
			{
				return EnvironmentHelper.domainDnsName;
			}
		}

		static EnvironmentHelper()
		{
			EnvironmentHelper.shortDomainName = EnvironmentHelper.GetShortDomainName();
			EnvironmentHelper.domainDnsName = EnvironmentHelper.GetDomainDnsName();
		}

		[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
		private static extern int GetComputerNameEx(EnvironmentHelper.COMPUTER_NAME_FORMAT NameType, StringBuilder nameBuffer, ref int bufferSize);

		[DllImport("secur32.dll", CharSet = CharSet.Auto)]
		private static extern int GetComputerObjectName(EnvironmentHelper.EXTENDED_NAME_FORMAT nameFormat, StringBuilder nameBuffer, ref int bufferSize);

		private static bool CheckIsWebApplication()
		{
			bool result = false;
			AppDomain currentDomain = AppDomain.CurrentDomain;
			try
			{
				if (currentDomain.ShadowCopyFiles)
				{
					result = (HttpContext.Current != null);
				}
			}
			catch (Exception)
			{
			}
			return result;
		}

		private static string GetShortDomainName()
		{
			string text = EnvironmentHelper.InnerGetComputerObjectName(EnvironmentHelper.EXTENDED_NAME_FORMAT.NameSamCompatible);
			string[] array = text.Split(new char[]
			{
				'\\',
				'/'
			});
			return array[0];
		}

		private static string GetDomainDnsName()
		{
			return EnvironmentHelper.InnerGetComputerName(EnvironmentHelper.COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsDomain);
		}

		private static string InnerGetComputerObjectName(EnvironmentHelper.EXTENDED_NAME_FORMAT nameFormat)
		{
			StringBuilder stringBuilder = new StringBuilder(1024);
			int capacity = stringBuilder.Capacity;
			EnvironmentHelper.GetComputerObjectName(nameFormat, stringBuilder, ref capacity);
			return stringBuilder.ToString();
		}

		private static string InnerGetComputerName(EnvironmentHelper.COMPUTER_NAME_FORMAT nameType)
		{
			StringBuilder stringBuilder = new StringBuilder(1024);
			int capacity = stringBuilder.Capacity;
			EnvironmentHelper.GetComputerNameEx(nameType, stringBuilder, ref capacity);
			return stringBuilder.ToString();
		}
	}
}
